package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.listener.OnCommoditiesListListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-28
 * Time: 18:34
 */

public class CommoditiesListCallback extends Callback<CommodityList> {

    private OnCommoditiesListListener onCommoditiesListListener;

    public CommoditiesListCallback(OnCommoditiesListListener onCommoditiesListListener){
        this.onCommoditiesListListener = onCommoditiesListListener;
    }

    @Override
    public CommodityList parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        CommodityList commodityList=new Gson().fromJson(string,CommodityList.class);
        return commodityList;
    }

    @Override
    public void onError(Request request, Exception e) {
        onCommoditiesListListener.showCommoditiesListFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(CommodityList commodityList) {
        if(commodityList.getStatus()==0)
            onCommoditiesListListener.showCommoditiesListSucc(commodityList);
        else
            onCommoditiesListListener.showCommoditiesListFail(commodityList.getErrorMessage());
    }
}
